﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.XT;
using Rison.BusinessFacade.Facade.XT;
using Rison.DataAccess.Common;

namespace Rison.Web.admin
{
    public partial class SysUserEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(labMsg.Text)) labMsg.Text = string.Empty;

            if (!this.IsPostBack)
            {
                if (base.IsEdit)
                {
                    LoadData();
                }
            }
        }


        private void LoadData()
        {
            SysUserFacade facade = new SysUserFacade();
            SysUser user = facade.Retrieve(this.key, true) as SysUser;
            if (user != null)
            {
                this.txtUserId.Text = user.UserId;
                this.txtName.Text = user.Name;
                if (string.IsNullOrEmpty(user.Sex) || "男".Equals(user.Sex))
                    this.rblSex.SelectedIndex = 0;
                else
                    this.rblSex.SelectedIndex = 1;
                this.txtDescription.Text = user.Description;

                if (this.txtUserId.Text == "admin")
                    this.txtUserId.Enabled = false;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SysUserView.aspx");
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (System.Text.Encoding.Default.GetByteCount(txtUserId.Text.Trim()) > 30)
                this.labMsg.Text = "用户ID不能超过30个字符！";
            else if (!StringHelper.IsMergeByNumberAndChar(txtUserId.Text.Trim(), "_"))
                this.labMsg.Text = "用户ID只能有数字、字母和下划线组成！";
            else if (System.Text.Encoding.Default.GetByteCount(txtName.Text.Trim()) > 50)
                this.labMsg.Text = "姓名不能超过50个字符！";
            else if (System.Text.Encoding.Default.GetByteCount(txtDescription.Text.Trim()) > 500)
                this.labMsg.Text = "备注信息不能超过500个字符！";
            else
            {
                SysUserFacade facade = new SysUserFacade();

                List<SqlField> indexKeyFields = new List<SqlField>();
                indexKeyFields.Add(new SqlField("UserId", txtUserId.Text.Trim()));

                if (facade.IndexKeyExists((base.IsEdit) ? base.key : "0", indexKeyFields))
                    this.labMsg.Text = "当前用户ID已存在，请重新输入！";
                else
                {
                    SysUser user = null;
                    if (base.IsEdit)
                        user = facade.Retrieve(Convert.ToInt32(base.key)) as SysUser;
                    else
                        user = new SysUser();

                    user.UserId = txtUserId.Text.Trim();
                    user.Name = txtName.Text;
                    user.Sex = rblSex.SelectedValue;
                    user.Description = txtDescription.Text;

                    long returnValue = 0;
                    if (base.IsEdit)
                    {
                        returnValue = facade.Update(user);
                    }
                    else
                        returnValue = facade.Save(user);

                    if (returnValue > 0)
                    {
                        this.labMsg.Text = "保存完成！";
                    }
                    else
                        this.labMsg.Text = "保存出错！";
                }
            }
        }


    }
}
